package com.shane.andtools.perf;

import android.text.TextUtils;
import android.util.Log;

import com.shane.andtools.MainActivity;


/**
 * Created by shane on 2018/1/25.
 */

public class Test {
    static final boolean DEBUG = false;
    static int sPassCount = 0;
    static int sFailedCount = 0;
    public static void test() {
        Doraemon.setRunningMode(Doraemon.MODE_GRAY);
        DDStringBuilder s1 = DDStringBuilderProxy.getDDStringBuilder();
        s1.append("s1");
//        Log.e(MainActivity.TAG, "s1.length = " + s1.length());

        DDStringBuilder s2 = DDStringBuilderProxy.getDDStringBuilder();
        s2.append("");
//        Log.e(MainActivity.TAG, "s2.length = " + s2.length());

        DDStringBuilder s3 = DDStringBuilderProxy.getDDStringBuilder();
        s3.append("s3");
        s3.append("s4");
//        Log.e(MainActivity.TAG, "s3.length = " + s3.length());

//        Log.e(MainActivity.TAG, "s1.length = " + s1.length());
//        Log.e(MainActivity.TAG, "s2.length = " + s2.length());
//        Log.e(MainActivity.TAG, "s3.length = " + s3.length());

        DDStringBuilder s4 = DDStringBuilderProxy.getDDStringBuilder();
        s4.append("s1");

        DDStringBuilder s5 = DDStringBuilderProxy.getDDStringBuilder();
        s5.append("");

        DDStringBuilder s6 = DDStringBuilderProxy.getDDStringBuilder();
        s6.append("s3");
        s6.append("s4");

        log(s1.toString(), s2.toString(), s3.toString());
        log(s4.toString(), s5.toString(), s6.toString());
    }

    public static void test(int total) {
        sPassCount = 0;
        sFailedCount = 0;
        if (total < 1) {
            return;
        }

        for (int i = 0; i < total; i++) {
            if (DEBUG) {
                Log.e(MainActivity.TAG, "index = " + i);
            }

            test();
        }
    }

    private static void log(String s1, String s2, String s3) {
        if (DEBUG) {
            Log.e(MainActivity.TAG, "s1:" + s1 + ", s2:" + s2 + ", s3:" + s3);
        }

        int size = DDStringBuilderProxy.getDDStringBuilderPoolSize();
        if (TextUtils.equals(s1, "s1") && TextUtils.equals(s2, "") && TextUtils.equals(s3, "s3s4")) {
            ++sPassCount;
            if (DEBUG) {
                Log.e(MainActivity.TAG, "----------------------------PASS------------------size:" + size + ", sPassCount: " + sPassCount);
            }
            if (sPassCount%100 == 0) {
                Log.e(MainActivity.TAG, "----------------------------PASS------------------size:" + size + ", sPassCount: " + sPassCount);
            }
        } else {
            ++sFailedCount;
            Log.e(MainActivity.TAG, "s1:" + s1 + ", s2:" + s2 + ", s3:" + s3);
            Log.e(MainActivity.TAG, "----------------------------FAILED------------------size:" + size + ", sFailedCount: " + sFailedCount);
        }
    }
}
